<template>
  <div class="container">
    <h3 class="title">
      <svg-icon icon-class="list" class="title-icon" />订单管理
    </h3>
    <el-card>
      <div slot="header">
        <span>订单管理</span>
      </div>
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="客户姓名">
          <el-input v-model="formInline.userName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="客户手机号">
          <el-input v-model="formInline.userPhone" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="formInline.userIdCard" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="销售单ID">
          <el-input v-model="formInline.salesOrderId" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="项目">
          <el-select v-model="formInline.projectId" placeholder="请选择">
            <el-option label="请选择" value></el-option>
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item, index) in options"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="date"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleSearch">查询</el-button>
          <el-button type="primary" size="small">
            <a :href="href" download>导出</a>
          </el-button>
        </el-form-item>
      </el-form>
      <div style="margin-bottom:10px;">
        <el-table
          :data="tableData"
          height="calc(100vh - 410px)"
          border
          stripe
          style="width: 100%;margin-bottom:10px;"
        >
          <el-table-column prop="id" label="序号"></el-table-column>
          <el-table-column prop="orderNo" label="订单编号"></el-table-column>
          <el-table-column prop="userName" label="客户姓名"></el-table-column>
          <el-table-column prop="userPhone" label="手机号"></el-table-column>
          <el-table-column prop="userIdCard" label="身份证"></el-table-column>
          <el-table-column prop="salesOrderId" label="销售单ID"></el-table-column>
          <el-table-column prop="projectName" label="项目"></el-table-column>
          <el-table-column label="房号">
            <template slot-scope="scope">{{scope.row.loudong}}栋{{scope.row.roomNo}}号</template>
          </el-table-column>
          <el-table-column prop="moneyType" label="款项类型"></el-table-column>
          <el-table-column prop="actualAmountReceived" label="金额"></el-table-column>
          <el-table-column label="付款时间">
            <template slot-scope="scope">{{scope.row.transactionHour|getTime}}</template>
          </el-table-column>
          <el-table-column width="150" label="操作">
            <template slot-scope="scope">
              <el-button type="text" @click="handleDetail(scope.row.id)">详情</el-button>
              <el-button
                type="text"
                style="color:red"
                v-if="scope.row.salesOrderId==''||scope.row.salesOrderId==null"
                @click="contact(scope.row.id)"
              >关联销售单</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalPage"
          style="float: right;"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getProject } from "@/api/money";
import { getList, getExport, addLocalOrder } from "@/api/order";
import { getToken } from "@/utils/auth";
export default {
  name: "order",
  filters: {
    getTime(timestamp) {
      var time = new Date(timestamp);
      var y = time.getFullYear(); //getFullYear方法以四位数字返回年份
      var M = time.getMonth() + 1; // getMonth方法从 Date 对象返回月份 (0 ~ 11)，返回结果需要手动加一
      M = m < 10 ? "0" + M : M;
      var d = time.getDate(); // getDate方法从 Date 对象返回一个月中的某一天 (1 ~ 31)
      d = d < 10 ? "0" + d : d;
      var h = time.getHours(); // getHours方法返回 Date 对象的小时 (0 ~ 23)
      h = h < 10 ? "0" + h : h;
      var m = time.getMinutes(); // getMinutes方法返回 Date 对象的分钟 (0 ~ 59)
      m = m < 10 ? "0" + m : m;
      var s = time.getSeconds(); // getSeconds方法返回 Date 对象的秒数 (0 ~ 59)
      s = s < 10 ? "0" + s : s;
      return y + "-" + M + "-" + d + " " + h + ":" + m + ":" + s;
    }
  },
  data() {
    return {
      formInline: {
        projectId: "",
        date: "",
        salesOrderId: "",
        userIdCard: "",
        userPhone: "",
        userName: ""
      },
      href: "",
      options: [],
      page: 1,
      pageSize: 10,
      totalPage: 0,
      tableData: [],
      startTime: "",
      endTime: "",
      date: ""
    };
  },
  mounted() {
    this.getOptions();
    if (this.date) {
      this.startTime = this.date[0];
      this.endTime = this.date[1];
    } else {
      this.startTime = "";
      this.endTime = "";
    }
    var token = getToken();
    this.href =
      process.env.VUE_APP_BASE_API +
      "/pc/management/order/export?startTime=" +
      this.startTime +
      "&endTime=" +
      this.endTime +
      "&projectId=" +
      this.formInline.projectId +
      "&userName=" +
      this.formInline.userName +
      "&userPhone=" +
      this.formInline.userPhone +
      "&salesOrderId=" +
      this.formInline.salesOrderId +
      "&token=" +
      token;
    this.getData();
  },
  methods: {
    contact(id, no) {
      this.$router.push({
        path: "/order/contact",
        query: {
          id: id
        }
      });
    },
    handleSearch() {
      this.page = 1;
      this.getData();
    },
    getOptions() {
      getProject().then(res => {
        this.options = res.data;
      });
    },
    handleDetail(id) {
      this.$router.push({ path: "/order/detail", query: { id: id } });
    },
    getData() {
      if (this.date) {
        this.startTime = this.date[0];
        this.endTime = this.date[1];
      } else {
        this.startTime = "";
        this.endTime = "";
      }
      getList(
        this.startTime,
        this.endTime,
        this.formInline.userName,
        this.page,
        this.pageSize,
        this.formInline.projectId,
        this.formInline.salesOrderId,
        this.formInline.userIdCard,
        this.formInline.userPhone
      ).then(res => {
        this.tableData = res.data;
        this.totalPage = res.recordsTotal;
      });
    },
    handleEdit() {
      this.$router.push({ path: "/project/edit" });
    },
    handleImport() {
      if (this.date) {
        this.startTime = this.date[0];
        this.endTime = this.date[1];
      } else {
        this.startTime = "";
        this.endTime = "";
      }
      this.href =
        process.env.VUE_APP_BASE_API +
        "/api/pc/management/order/export?startTime=" +
        this.startTime +
        "&endTime=" +
        this.endTime +
        "&projectId=" +
        this.formInline.projectId +
        "&userName=" +
        this.formInline.userName +
        "&userPhone=" +
        this.formInline.userPhone +
        "&salesOrderId=" +
        this.formInline.salesOrderId;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getData();
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
  background-color: rgb(240, 242, 245);
  position: relative;
  .title {
    font-size: 14px;
    font-weight: 400;
    .title-icon {
      font-size: 14px;
      display: inline-block;
      margin-right: 5px;
    }
  }
  /deep/ .el-card__header {
    padding: 18px 0;
    margin: 0 20px;
  }
}
.form-inline {
  border-bottom: 1px solid #e6ebf5;
  margin-bottom: 10px;
}
</style>
